package com.controller.search 
{
	import com.events.search.ProductSearchEvent;
	import com.model.valueObjects.VoProductTotalStock;
	import com.service.ProductSearch;
	import com.view.Searching.com.OnHandLinksPopup;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ProductSearchController extends AbstractController
	{
		
		[Inject]
		public var productSearch:ProductSearch;
		
		public function ProductSearchController()
		{
			super();
		}
		
		[EventHandler(event="ProductSearchEvent.GET_PRO_ONHAND_LINKS",properties="pro_serialnumber")]
		public function getProOnhandLinks(pro_serialnumber:Number):void{
			model.proOnHandLinks = new ArrayCollection();
			sh.executeServiceCall(productSearch.getOnHandStockLinks(pro_serialnumber),getProOnhandLinksResult, handleFault );
		}		
		private function getProOnhandLinksResult(event:ResultEvent):void
		{
			model.proOnHandLinks = event.result as ArrayCollection;			
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="ProductSearchEvent.GET_PRO_SOLD_LINKS",properties="pro_serialnumber")]
		public function getProSoldLinks(pro_serialnumber:Number):void{
			model.proSoldLinks = new ArrayCollection();
			sh.executeServiceCall(productSearch.getSoldLinks(pro_serialnumber),getProSoldLinksResult, handleFault );
		}		
		private function getProSoldLinksResult(event:ResultEvent):void
		{
			model.proSoldLinks = event.result as ArrayCollection;			
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="ProductSearchEvent.GET_PRO_ONSALE_LINKS",properties="pro_serialnumber")]
		public function getProOnSaleLinks(pro_serialnumber:Number):void{
			model.proOnSaleLinks = new ArrayCollection();
			sh.executeServiceCall(productSearch.getOnSaleStockLinks(pro_serialnumber),getProOnSaleLinksResult, handleFault );
		}	
		
		private function getProOnSaleLinksResult(event:ResultEvent):void
		{
			model.proOnSaleLinks = event.result as ArrayCollection;			
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="ProductSearchEvent.GET_PRO_INORDER_LINKS",properties="pro_serialnumber")]
		public function getProInOrderLinks(pro_serialnumber:Number):void{
			model.proInOrderLinks = new ArrayCollection();
			sh.executeServiceCall(productSearch.getInOrderLinks(pro_serialnumber),getProInOrderLinksResult, handleFault );
		}	
		
		private function getProInOrderLinksResult(event:ResultEvent):void
		{
			model.proInOrderLinks = event.result as ArrayCollection;			
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}